<?php


namespace app\beautiful\controller\admin\coupon;


use app\BaseController;
use app\beautiful\model\CouponModel;
use app\common\logic\DataLogic;

class Coupon extends BaseController
{

    public function cate(){
        $search = $this->request->param('search');
        $list[] = [
            'type_id'=>1,
            'name'=>'普通券'
        ];
        $list[] = [
            'type_id'=>2,
            'name'=>'新人券'
        ];
        $list[] = [
            'type_id'=>3,
            'name'=>'会员券'
        ];
        $list[] = [
            'type_id'=>4,
            'name'=>'邀请券'
        ];
        $list[] = [
            'type_id'=>5,
            'name'=>'礼品券'
        ];
        if(!empty($search)){
            $where[] = ['name','like','%'.$search.'%'];

            $cate = self::searchArray($list,$search);

            $return['list'] = $cate;
        }else{
            $return['list'] = $list;
        }



        return $this->success($return);
    }

    public function index()

    {
        $search = $this->request->param('search');
        $type_id = $this->request->param('type_id/d',0);
        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

            ['status','=',1],

            ['type_id','=',$type_id],

        ];

        if(!empty($search)) $where[]=['name','like','%'.$search.'%'];

        $coupon=new CouponModel();

        $order=['update_time'=>'desc','add_time'=>'desc'];

        $field="coupon_id,name,type_id,valid_bg_time,valid_end_time,coupon_money,money_limit,add_time";

        $append=['add_time_format'];

        $return = DataLogic::getDataList($coupon,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        return $this->success($return);

    }

    static function searchArray($array, $keyword){
        $result = array();
        foreach ($array as $value) {
            if (stripos($value['name'], $keyword) !== false) {
                $result[] = $value;
            }
        }
        return $result;
    }

}